package com.csun.cmny.provider.controller;

import cn.jiguang.common.utils.Preconditions;
import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcExpenseType;
import com.csun.cmny.provider.model.dto.ExpenseTypeDto;
import com.csun.cmny.provider.model.vo.ExpenseTypeVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcExpenseTypeService;
import com.csun.cmny.provider.validator.OmcExpenseTypeAddValidator;
import com.csun.cmny.provider.validator.OmcExpenseTypeEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/expenseType", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcExpenseTypeController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcExpenseTypeService omcExpenseTypeService;

    /**
     * 消费类别-添加
     *
     * @param: [expenseTypeDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addExpenseType(@Validated(OmcExpenseTypeAddValidator.class)
                              @RequestBody ExpenseTypeDto expenseTypeDto, BindingResult br){

        int ret = saveExpenseType(expenseTypeDto,getLoginAuthDto());

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 消费类别-基础类别添加
     *
     * @param: [expenseTypeDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/baseTypeAdd")
    public Wrapper baseTypeAdd(@RequestParam("groupId") Long groupId){

        int ret = omcExpenseTypeService.baseTypeAdd(groupId,getLoginAuthDto());

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 消费类别-修改
     *
     * @param: [expenseTypeDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editExpenseType(@Validated(OmcExpenseTypeEditValidator.class)
                                  @RequestBody ExpenseTypeDto expenseTypeDto,
                                   BindingResult br){

        int ret = saveExpenseType(expenseTypeDto,getLoginAuthDto());

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 查询所有消费类别及消费项目
     *
     * @param: []
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryAll")
    public Wrapper queryAll(@RequestBody ExpenseTypeDto expenseTypeDto){

        List<ExpenseTypeVo> expenseTypeVoList = omcExpenseTypeService.queryAll(expenseTypeDto,getLoginAuthDto());

//        return WrapMapper.ok(expenseTypeVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), expenseTypeVoList);
    }

    /**
     * 消费类别-查询
     *
     * @param: [expenseTypeDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/get")
    public Wrapper getExpenseType(@RequestBody ExpenseTypeDto expenseTypeDto){

        OmcExpenseType omcExpenseType = new OmcExpenseType();
        BeanUtils.copyProperties(expenseTypeDto,omcExpenseType);
        OmcExpenseType expenseType = omcExpenseTypeService.selectByKey(expenseTypeDto.getId());

//        return WrapMapper.ok(expenseType);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), expenseType);
    }


    /**
     * 消费项目-删除
     *
     * @param: [expenseItemId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteById/{expenseTypeId}")
    public Wrapper deleteExpenseItem(@PathVariable Long expenseTypeId){

        //校验expenseItemId是否为空
        Preconditions.checkArgument(expenseTypeId != null, cmnyMessageSource
                .getMessage("ExpenseItem.id.null", null));

        Integer result = omcExpenseTypeService.deleteExpenseTypeById(expenseTypeId,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }


    /**
     * 消费类别-保存
     *
     * @param: [expenseTypeDto, loginAuthDto]
     * @return: java.lang.Integer
     */
    public Integer saveExpenseType(ExpenseTypeDto expenseTypeDto, LoginAuthDto loginAuthDto){
        OmcExpenseType omcExpenseType = new OmcExpenseType();
      BeanUtils.copyProperties(expenseTypeDto,omcExpenseType);
        return omcExpenseTypeService.saveExpenseType(omcExpenseType,loginAuthDto);
    }

}
